package net.pws.oos.web.dto;

import java.util.ArrayList;
import java.util.List;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.SupervisionRecod;
import net.pws.oos.biz.model.Task;

import org.apache.commons.lang.StringUtils;

public class SupervDto extends SupervisionRecod {

	private static final long serialVersionUID = 1L;

	public static List<SupervDto> from(List<SupervisionRecod> supervList) {
		List<SupervDto> dto = new ArrayList<SupervDto>();
		for (SupervisionRecod superv : supervList) {
			dto.add(from(superv));
		}
		return dto;
	}

	public static SupervDto from(SupervisionRecod superv) {
		String[] ignoredProps = { "task" };

		SupervDto dto = new SupervDto();
		BeanUtils.copyProperties(dto, superv, ignoredProps);

		// District district = task.getDistrict();
		// if (district != null) {
		// dto.setDistrictId(district.getId());
		// dto.setDistrictName(district.getName());
		// }

		Task task = superv.getTask();
		if (task != null) {
			dto.setTaskId(task.getId());
			dto.setTaskContentOfTask(task.getContent());
			dto.setTaskDescriptionOfTask(task.getDescription());
			
		}

		// TaskResult result = task.getResult();
		// if (result != null) {
		// dto.setResultId(result.getId());
		// }

		return dto;
	}

	// public static TaskDto from(Task task, List<Task> children) {
	// TaskDto dto = from(task);
	// List<TaskDto> subDtos = from(children);
	// dto.setChildren(subDtos);
	// return dto;
	// }

	public static SupervisionRecod to(SupervisionRecod superv, SupervDto dto, EntityManager entityManager) {
		if (superv == null) {
			superv = new SupervisionRecod();
		}

		String[] ignoredProps = { "id","task" };
		BeanUtils.copyProperties(superv, dto, ignoredProps);

		// if (StringUtils.isEmpty(dto.getCreatorId())) {
		// task.setCreator(null);
		// } else {
		// task.setCreator(entityManager.getReference(User.class,
		// dto.getCreatorId()));
		// }

		// if (StringUtils.isEmpty(dto.getDistrictId())) {
		// task.setDistrict(null);
		// }
		// else {
		// task.setDistrict(entityManager.find(District.class,
		// dto.getDistrictId()));
		// }

		if (StringUtils.isEmpty(dto.getTaskId())) {
			superv.setTask(null);
		} else {
			superv.setTask(entityManager.find(Task.class, dto.getTaskId()));
		}

		return superv;
	}

	private String taskId;
	
	private String taskContentOfTask;

	private String taskDescriptionOfTask;

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getTaskContentOfTask() {
		return taskContentOfTask;
	}

	public void setTaskContentOfTask(String taskContentOfTask) {
		this.taskContentOfTask = taskContentOfTask;
	}

	public String getTaskDescriptionOfTask() {
		return taskDescriptionOfTask;
	}

	public void setTaskDescriptionOfTask(String taskDescriptionOfTask) {
		this.taskDescriptionOfTask = taskDescriptionOfTask;
	}
	
	
	
	
}
